package com.macro.mall.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.mapper.UmsMemberExtendMapper;
import com.macro.mall.mapper.UmsMemberMapper;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberAll;
import com.macro.mall.model.UmsMemberExample;
import com.macro.mall.model.UmsMemberExtend;
import com.macro.mall.security.service.RedisService;
import com.macro.mall.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

@Service
public class UmsMemberServiceImpl implements UmsMemberService {

    //验证密码，至少为字母、数字、符号两种组成，不包含空格,不能输入中文
    private static Pattern NUMBER_PATTERN = Pattern.compile("(?!^\\d+$)(?!^[A-Za-z]+$)(?!^[^A-Za-z0-9]+$)(?!^.*[\\u4E00-\\u9FA5].*$)^\\S{8,12}$");
    @Autowired
    private UmsMemberMapper umsMemberMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RedisService redisService;
    @Autowired
    private UmsMemberExtendMapper memberExtendMapper;
    /**
     * 保存
     * 所有字段全都保存
     *
     * @param umsMember
     * @return 保存后的对象包括ID
     */
    @Override
    public int saveUmsMember(UmsMember umsMember) {
        /**if(umsMember.getId()==null||umsMember.getId()==""){
         *      String uuid =UUID.randomUUID().toString().replace("-", "").trim();
         *     umsMember.setId(uuid);
         */
        return umsMemberMapper.insert(umsMember);
    }

    /**
     * 保存
     * 判断为空保存
     *
     * @param umsMember
     * @return 保存后的对象包括ID
     */
    @Override
    public int saveUmsMemberSelective(UmsMember umsMember) {
        /**if(umsMember.getId()==null||umsMember.getId()==""){
         *      String uuid =UUID.randomUUID().toString().replace("-", "").trim();
         *     umsMember.setId(uuid);
         */
        return umsMemberMapper.insertSelective(umsMember);
    }

    /**
     * 根据ID删除
     *
     * @param id 主键ID
     * @return 删除记录数
     */
    @Override
    public int deleteUmsMemberById(String id) {

        return umsMemberMapper.deleteByPrimaryKey(Long.parseLong(id));
    }

    /**
     * 更新
     * 更新所有字段
     *
     * @param umsMember
     * @return 保存后的对象包括ID
     */
    @Override
    public int updateUmsMember(UmsMember umsMember) {
        UmsMemberExample umsMemberExample = new UmsMemberExample();

        return umsMemberMapper.updateByExample(umsMember, umsMemberExample);
    }

    /**
     * 更新
     * 判断为空保存更新字段
     *
     * @param umsMember
     * @return 保存后的对象包括ID
     */
    @Override
    public int updateUmsMemberSelective(UmsMember umsMember) {

        if (!StrUtil.isEmpty(umsMember.getPassword())) {
            if (!NUMBER_PATTERN.matcher(umsMember.getPassword()).matches()) {
                Asserts.fail("密码中必须包含至少为字母、数字、符号两种组成长度为(8-12)位");
            }
            String newPassword = umsMember.getPassword();
            umsMember.setPassword(passwordEncoder.encode(newPassword));
        }
        UmsMemberExample umsMemberExample = new UmsMemberExample();
        if (null != umsMember.getId()) {
            umsMemberExample.createCriteria().andIdEqualTo(umsMember.getId());
        }
        int i = umsMemberMapper.updateByPrimaryKeySelective(umsMember);
        redisService.del("mall:ums:member:" + umsMember.getUsername());
        return i;

    }
    /**
     * 更新
     * 判断为空保存更新字段
     *
     * @param umsMember
     * @return 保存后的对象包括ID
     */
    @Override
    public int updateMemberSelective(UmsMember umsMember) {

        UmsMemberExample umsMemberExample = new UmsMemberExample();
        if (null != umsMember.getId()) {
            umsMemberExample.createCriteria().andIdEqualTo(umsMember.getId());
        }
        int i = umsMemberMapper.updateByPrimaryKeySelective(umsMember);
        redisService.del("mall:ums:member:" + umsMember.getUsername());
        return i;

    }
    /**
     * 按ID查询
     *
     * @return UmsMember
     * @parm id 主键ID
     */
    @Override
    public UmsMember selectUmsMemberById(String id) {

        return umsMemberMapper.selectByPrimaryKey(Long.parseLong(id));
    }


    /**
     * 按条件查询全部的
     *
     * @param umsMember 查询条件
     * @return List<UmsMember>
     */
    @Override
    public List<UmsMember> selectAllByUmsMember(UmsMember umsMember) {
        PageHelper.startPage(umsMember.getPageNum(), umsMember.getPageSize());
        UmsMemberExample umsMemberExample = new UmsMemberExample();
        UmsMemberExample.Criteria criteria = umsMemberExample.createCriteria();
        if(StrUtil.isNotEmpty(umsMember.getUsername())) {
            criteria.andUsernameEqualTo(umsMember.getUsername());
        }
        if(StrUtil.isNotEmpty(umsMember.getName())) {
            criteria.andNameLike("%" + umsMember.getName() + "%");
        }
        return umsMemberMapper.selectByExample(umsMemberExample);
    }

    @Override
    public List<UmsMember> selectAllByUmsMember(String username, String name, String community, Integer pageSize, Integer pageNum) {

        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("create_time desc");
        UmsMemberExample umsMemberExample = new UmsMemberExample();
        UmsMemberExample.Criteria criteria = umsMemberExample.createCriteria();
        if (!StrUtil.isEmpty(username)) {
            criteria.andUsernameEqualTo(username);
        }
        if (!StrUtil.isEmpty(name)) {
            criteria.andNameEqualTo(name);
        }
        if (StrUtil.isNotEmpty(community)) {
            criteria.andCommunityNameEqualTo(community);
        }
        return umsMemberMapper.selectByExample(umsMemberExample);
    }
    @Override
    public List<UmsMemberAll> listForCheckIn(UmsMemberAll memberAll,Integer pageSize, Integer pageNum) {

        PageHelper.startPage(pageNum, pageSize);

        return umsMemberMapper.listForCheckIn(memberAll);
    }
    @Override
    public UmsMemberAll getMemberAllById(Long id) {
        UmsMemberAll  queryMemberAll = new UmsMemberAll();
        queryMemberAll.setId(id);
        List<UmsMemberAll> list =  umsMemberMapper.listForCheckIn(queryMemberAll);
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            UmsMember member = umsMemberMapper.selectByPrimaryKey(id);
            UmsMemberAll  memberAll = new UmsMemberAll();
            if(member!=null){
                BeanUtils.copyProperties(member,memberAll);
            }
            return memberAll;
        }
    }
    @Override
    public void delCheckInByExtendId(Long extendId) {
        UmsMemberExtend record = new UmsMemberExtend();
        record.setId(extendId);
        record.setExtendStauts("1");
        memberExtendMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void delVolunteerByExtendId(Long extendId) {
        UmsMemberExtend record = new UmsMemberExtend();
        record.setId(extendId);
        record.setIsVolunteer("N");
        memberExtendMapper.updateByPrimaryKeySelective(record);
    }

}